package com.itheima.mytest;

import java.io.*;
import java.util.ArrayList;
import java.util.Comparator;

public class Test06Case1 {
    public static void main(String[] args) throws IOException {
        /*
                需求：把《出师表》的文章顺序进行恢复到一个新文件中。
            */

        //1.创建对象
        BufferedReader br = new BufferedReader(new FileReader("myio\\csb.txt"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("myio\\copycsb.txt"));
        //2.读入数据并添加到集合当中
        ArrayList<String> list = new ArrayList<>();
        String line;
        while ((line = br.readLine()) != null) {
            list.add(line);
        }

        //3.排序
        //排序规则；按照每一行前面的序号进行排列
        list.sort(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return Integer.parseInt(o1.split("\\.")[0]) - Integer.parseInt(o2.split("\\.")[0]);
            }
        });

        //4.写出数据
        for (String s : list) {
            bw.write(s);
            bw.newLine();
        }

        //5.资源释放
        bw.close();
        br.close();
    }
}
